package com.linkivr.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.linkivr.util.GL;
import com.linkivr.model.*;
import com.linkivr.service.*;


@Component("unitAction")
public class UnitAction extends MyAction {
	private static final long serialVersionUID = 1L;
	private UnitService service;

	public UnitService getService() {
		return service;
	}

	@Resource
	public void setService(UnitService service) {
		this.service = service;
	}

	public String list() throws IOException {
		GL.info("user.list()函数  ");
		beginReqParam();
		// 获取表达参数

		List<T_unit> tabObjs = service.queryAll(start, limit);

		jsonString = "{totalProperty:" + service.queryCount() + ",root:[";
		int i = 0;
		for (T_unit to : tabObjs) {

			if (i != 0 && i != tabObjs.size())
				jsonString = jsonString + ",";
			i++;
			String t = "{unit_id:'" + to.getUnit_id() + "',unit_name:'" + to.getUnit_name() + "',unit_memo:'"
					+ to.getUnit_memo() + "'}";
			jsonString = jsonString + t;
		}
		jsonString += "]}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String listAll() throws Exception {
		GL.info("listAll()函数  ");
		beginReqParam();
		// ApplicationContext ac = new
		// ClassPathXmlApplicationContext("beans.xml");
		List<T_unit> complist = service.queryAll();
		JSONArray array = JSONArray.fromObject(complist);
		out.write(array.toString());
		endReqParam();
		return null;
	}

	public String query() throws IOException {
		GL.info("query()函数  ");
		String jsonString = "";
		beginReqParam();

		String p_id = request.getParameter("p_id");

		T_unit tabObj = service.query(Integer.parseInt(p_id));
		GL.info("query()函数 ok p_id= " + p_id);
		jsonString = "{success: true ,data:{f_unit_name:'" + tabObj.getUnit_name() + "',f_unit_memo:'"
				+ tabObj.getUnit_memo() + "'}}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String add() throws IOException {
		GL.info("add()函数  ");
		String jsonString = "";
		beginReqParam();

		String f_unit_name = request.getParameter("f_unit_name");
		String f_unit_memo = request.getParameter("f_unit_memo");

		T_unit to = new T_unit();
		to.setUnit_name(f_unit_name);
		to.setUnit_memo(f_unit_memo);

		service.add(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String delete() throws IOException {
		GL.info("delete()函数  ");
		String jsonString = "";
		beginReqParam();

		String f_data = request.getParameter("f_data");
		JSONObject a = null;
		Object bean = null;
		JSONArray array = JSONArray.fromObject(f_data);
		Object[] objArray = array.toArray();
		for (Object obj : objArray) {
			a = JSONObject.fromObject(obj);
			bean = JSONObject.toBean(a);
			// 经过查看，发现 bean 为 MorphDynaBean 类型
			MorphDynaBean morphDynaBean = (MorphDynaBean) bean;
			String id = (String) morphDynaBean.get("unit_id");
			service.delete(Integer.parseInt(id));

		}
		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String update() throws IOException {
		GL.info("update()函数  ");
		String jsonString = "";
		beginReqParam();

		String p_id = request.getParameter("p_id");

		String f_unit_name = request.getParameter("f_unit_name");
		String f_unit_memo = request.getParameter("f_unit_memo");

		T_unit to = service.query(Integer.parseInt(p_id));
		to.setUnit_name(f_unit_name);
		to.setUnit_memo(f_unit_memo);

		service.update(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

}
